import os
import requests
from urllib.parse import urlparse


def create_dir(folder_name):
    prefix = 'downloaded_images/toutiao/'
    # 创建文件夹
    folder_name = prefix + folder_name
    if not os.path.exists(folder_name):
        os.makedirs(folder_name)
        print(f"Folder '{folder_name}' created.")
    else:
        print(f"Folder '{folder_name}' already exists.")


def saved_images(folder_name, image_urls):
    prefix = 'downloaded_images/toutiao/'
    create_dir(folder_name)
    # 下载并保存图片
    for url in image_urls:
        try:
            # 发送 HTTP GET 请求
            response = requests.get(url)
            response.raise_for_status()  # 检查请求是否成功

            # 提取文件名
            parsed_url = urlparse(url)
            file_name = os.path.basename(parsed_url.path)

            # 保存图片到本地
            file_path = os.path.join(prefix + folder_name, file_name)
            with open(file_path, 'wb') as file:
                file.write(response.content)

            print(f"Image saved: {file_path}")

        except requests.RequestException as e:
            print(f"Failed to download {url}: {e}")
        except Exception as e:
            print(f"An error occurred: {e}")

    print("All images have been downloaded and saved.")


#if __name__ == "__main__":
    # image_urls = [
    #     'https://p3-sign.toutiaoimg.com/tos-cn-i-axegupay5k/6a204a875b8449a5b00db153aa1b3ddd~tplv-tt-origin-web:gif.jpeg?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1733145451&x-signature=a7giXSjAw4j0Bq85wuq6jOMT59w%3D',
    #     'https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/17cb4b315aab431eb337b978fd622413~tplv-tt-origin-web:gif.jpeg?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1733145451&x-signature=sjE8a0uaO8oiaAmGWDIrarVxdTE%3D',
    #     'https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/c1cc8f4f151144e78834f84fa3959186~tplv-tt-origin-web:gif.jpeg?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1733145451&x-signature=OqFjYFvpFZrNYbCmVo%2Fomqhk0cw%3D',
    #     'https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/9a6ae63fa3814d1d833e502088372610~tplv-tt-origin-web:gif.jpeg?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1733145451&x-signature=3dTTcbWB3qYEt8m6%2BK9G7eH2pKo%3D',
    #     'https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/5612d19fd3614a2db9fd1f9e76234536~tplv-tt-origin-web:gif.jpeg?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1733145451&x-signature=vvEE8kFtiIO8OsEpTfKRb%2FudaW4%3D',
    #     'https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/4d63e73a15f245518eec2c2463b1fb53~tplv-tt-origin-web:gif.jpeg?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1733145451&x-signature=va9Yu2SaQmG4ldZLndjUC0eOUy8%3D',
    #     'https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/b57cf095a138411ca37879d8a09ccb85~tplv-tt-origin-web:gif.jpeg?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1733145451&x-signature=r8gacKc7i45NDJghsbWQ45pm9jw%3D',
    #     'https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/488429fc9cf340ae927cab27e435f64f~tplv-tt-origin-web:gif.jpeg?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1733145451&x-signature=ggun7I1xt1th4GlF0kqjXmFSeWs%3D']
    # saved_images('test2', image_urls)
